<?php
namespace My\Model;

use Utuo\UtuoModel;

class MallAgentModel extends UtuoModel {
	
    static $agentLevel = array(
        '0' => '一级代理商',
        '1' => '二级代理商',
        '2' => '三级代理商',
    );
    
    // 自动验证
    protected $_validate = array(
    	array('wechat_uid', 'getWechatUid','设置要设为代理的用户不正确', self::MUST_VALIDATE, 'callback'),
    	array('password', 'require', '密码必须', self::MUST_VALIDATE, '', self::MODEL_INSERT),
    );
    
    // 自动赋值
    protected $_auto = array(
        array('begin_time', 'strtotime', self::MODEL_BOTH, 'function'),
        array('end_time', 'strtotime', self::MODEL_BOTH, 'function'),
    	array('password', 'hash_pwd', self::MODEL_BOTH, 'function'),
    );
    
    
//     protected function _before_insert(&$data, $options) {
//     	parent::_before_insert($data, $options);
//     }
    
    /**
     * 新增代理商用户后处理
     * @see \Think\Model::_after_insert()
     */
    protected function _after_insert($data, $options) {
        $id = $data[$this->getPk()];
        if ( false !== $id ) {
        	/* 添加管理员 */
        	M("MallAgentAdmin")->add( array(
        			'agent_id' => $id,
        			'name' => $data['name'],
        			'email' => $data['email'],
        			'password' => $data['password'],
        			'last_login_time' => 0,
        			'last_login_ip' => '',
        			'login_count' => 0,
        			'is_role' => 0,
        			'is_super' => 1,
        	));
        	/* 更新关系树 */
        	$parentId = $data['parent_id']*1;
        	if ( $parentId ) {
        		$tree = M("MallAgent")->where('id=%d and cid=%d', $parentId, $data['cid'])->getField('tree');
        		$tree = $id.','.$tree;
        	} else {
        		$tree = $id;
        	}
        	M("MallAgent")->where("id=%d", $id)->setField("tree", $tree);
            /* 设置用户为代理商类型  */
            $wechatUid = $data['wechat_uid'];
            $m = M("WechatUser");
            $wechatUser = $m->where("id=%d", $wechatUid)->find();
            $userRole = new UserRole();
            $type = $userRole->type($wechatUser['type'])->becomeTo(WechatUserModel::USER_TYPE_AGENT)->getType();
            $m->where("id=%d", $wechatUid)->setField('type', $type); 
            /* 设置用户申请代理商已通过  */
            if( $applyId = $_REQUEST['applyId'] ) {
            	$cid = session('id');
            	M("MallAgentApply")->where(array('cid' => $cid, 'id' => $applyId ))->setField('status', 1);
            }
        }
    }
    
    /**
     * 更新前处理
     * @see \Utuo\UtuoModel::_before_update()
     */
    protected function _before_update(&$data, &$options) {
    	unset($data['wechat_uid']); // 指定的用户id不可修改
    	return parent::_before_update($data, $options);
    }
    
    /**
     * 用户id验证
     */
    protected function getWechatUid() {
    	$id = I('id', 0, "intval");
    	if ( !$id ) { // 添加用户时
    		$wechatUid = I('wechat_uid', 0, "intval");
    		$wechatUser = M("WechatUser")->where("id=%d", $wechatUid)->find();
    		if ( $wechatUser ) {
    			if ( $wechatUser['type'] == WechatUserModel::USER_TYPE_AGENT ) {
    				$this->error['wechat_uid'] = "您选择的用户已是代理商";
    			} else {
    				return true;
    			}
    		} else {
    			$this->error['wechat_uid'] = "请先选择已绑定的微信用户";
    		}
    		return false;
    	} else { // 修改时
    		return true;
    	}
    }
    
}